<?php

require_once( '../system/smarty.inc' );

require_once( '../system/mdao/Class_M_COST_FLIER.php' );
require_once( '../system/mdao/Class_M_COST_OTHER.php' );
require_once( '../system/mdao/Class_M_OTHER.php' );
require_once( '../system/Class_ERROR.php' );
require_once( '../system/login.inc.php' );
require_once( '../system/aoki_m.php' );
require_once( '../system/Class_PWD.php' );
require_once( '../system/mdao/Class_M_USER.php' );

Class_PWD::valid_auth(AOKI_AUTH_COST_QR,$login['staff_auth']);

$dosearch = FALSE ;
$dao = new Class_M_COST_FLIER;
//ゾーンとエリアの計算開始
//require_once( '../system/cell_ajareks_sort.inc.php' );
//ゾーンとエリアの計算終了

if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ||
	 isset( $_GET['hist'] ) &&  (  $_GET['hist'] == "2" ) ) {
	 $forms =  $_SESSION['MSEARCH']['COST_LIST_SUM'];
}else {
	$forms = $_GET;
	$_SESSION['MSEARCH']['COST_LIST_SUM'] = $forms;
}

$login_auth = $login['staff_auth'];

if ($login_auth == 'G-2' ) {

	$area_cd   = $login['autoid'];
	$forms["sort"] = '2';
	$areauser = array($aja_cd => $login['staff_id']." ".$login['staff_name'] );

}else if ($login_auth == 'G-3' ) {
	$zone_cd   = $login['autoid'];
	$forms["sort"] = '3';
	$cellsort = array();
	$zoneuser = array($aja_cd => $login['staff_id']." ".$login['staff_name'] );
}else {
	$zoneuser  = array();
	$zoneuser  = Class_M_OTHER::get_ajaeks_array(PLEASE_SELECT_ARR, 3);
	$areauser = array();
	$areauser =  Class_M_OTHER::get_ajaeks_array(PLEASE_SELECT_ARR, 2);
	$zone_cd = array();
	if ( !isset( $forms['sort'])) {$forms["sort"]='3';}
}

$year4  = $forms['year4'];

if  (($forms["sort"] == '2') &&  $forms["area_aja"]) {
	$aja =  $forms['area_aja'] ;
}

if  (($forms["sort"] == '3') &&  $forms["zone_aja"]) {
	$aja =  $forms['zone_aja'] ;
}

//---CSVダウンロード　処理開始

if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ){
	$_GET["csv"] = "";
	$tmpfilename = "areaad".date("Ymd_His");
	header("Content-Type: application/octet-stream");
	header("Content-Disposition: attachment; filename=$tmpfilename.csv");
	
	$colums = array();
	$col_heads = array(
		'year4'          => array( '年度',  1),
		'week'           => array( '週名',  1),
		'now_qua'        => array( '経準高 本年',  1),
		'plane_qua'      => array( '経準高 計画',  1),
		'last_qua'       => array( '経準高 前年',  1),
		'plane_rate_qua' => array( '経準高 計画費',  1),
		'plane_inc_qua'  => array( '経準高 計画差異',  1),
		'last_rate_qua'  => array( '経準高 前年比',  1),
		'last_inc_qua'   => array( '経準高 前年差異',  1),
		'now_cost'       => array( '経費 本年',  1),
		'plane_cost'     => array( '経費 計画',  1),
		'last_cost'      => array( '経費 前年',  1),
		'plane_rate_cost'=> array( '経費 計画費',  1),
		'plane_inc_cost' => array( '経費 計画差異',  1),
		'last_rate_cost' => array( '経費 前年比',  1),
		'last_inc_cost'  => array( '経費 前年差異',  1),
		'now_li'         => array( '利益 本年',  1),
		'plane_li'       => array( '利益 計画',  1),
		'last_li'        => array( '利益 前年',  1),
		'plane_rate_li'  => array( '利益 計画費',  1),
		'plane_inc_li'   => array( '利益 計画差異',  1),
		'last_rate_li'   => array( '利益 前年比',  1),
		'last_inc_li'    => array( '利益 前年差異',  1)
		
	);
		
		
	//列名
	foreach ($col_heads as $k=>$v) {
		$colums["name"][]   = $k;
		$colums["head"][]   = $v[0];
		$colums["type"][$k] = $v[1];
	}
		$data = $dao->searchList($page , 0 ,$fminit,5,$year4, NULL, NULL , $aja );
		
		//表示ために整形開始
		$plane_sum   = 0 ;
		$now_sum     = 0 ;
		$last_sum   = 0 ;
		
		$plane_sum_qua   = 0 ;
		$now_sum_qua     = 0 ;
		$last_sum_qua   = 0 ;
		
		foreach ($data as $k => $v ) {
			$dispweek = '第'.$v["week_cd"].'週';
			if ( $v["[week_seq"] ) {
				$dispweek = $dispweek.$v["[week_seq"]."回";
			}
			$dispweek = $dispweek."(". Class_PWD::int8_to_mmdd($v["flier_date"]) .")";
			$data[$k]["week"]        = $dispweek;
			
			$plane_sum = $plane_sum +  $v["plane_cost"] + $v["plane_dm"];
			$now_sum   = $now_sum   +  $v["now_cost"]   + $v["nowe_dm"];
			$last_sum  = $last_sum  +  $v["last_cost"]  + $v["last_dm"];
			
			$data[$k]["plane_cost"]  = $plane_sum;
			$data[$k]["now_cost"]    = $now_sum;
			$data[$k]["last_cost"]   = $last_sum;
			
			$plane_sum_qua  = $plane_sum_qua  +  $v["plane_qua"];
			$now_sum_qua    = $now_sum_qua    +  $v["now_qua"];
			$last_sum_qua   = $last_sum_qua   +  $v["last_qua"];
			
			$data[$k]["plane_qua"] = $plane_sum_qua;
			$data[$k]["now_qua"]   = $now_sum_qua;
			$data[$k]["last_qua"]  = $last_sum_qua;
			
			$data[$k]["plane_li"]    = $plane_sum_qua - $plane_sum ;
			$data[$k]["now_li"]      = $now_sum_qua   - $now_sum  ;
			$data[$k]["last_li"]     = $last_sum_qua  - $last_sum ;
			
			Class_PWD::set_csv_rate_inc_by_nm(&$data, $k, "cost");
			Class_PWD::set_csv_rate_inc_by_nm(&$data, $k, "qua");
			Class_PWD::set_csv_rate_inc_by_nm(&$data, $k, "li");
			//$data[$k] = $v;
		}
	//データ整形完了
	$dao->output_csv($data, $colums);
	exit;
}
//---CSVダウンロード　処理完了

	if ( isset( $forms['search'] ) && (  !empty( $forms['search'] ) ) ){
		$dosearch = TRUE;
	}

	if( isset( $forms['page'] ) && is_numeric( $forms['page'] ) ){
		$dosearch = TRUE;
		$page['current'] = $forms['page'];
	}else{
		$page['current'] = 1;
	}

	$page['item'] = PAGE_MAX;
	if ($dosearch) {
		//$matr = Class_M_OTHER::get_by_matrix_cd( $forms['week_cd']);
		
		//$page内容は関数内部更新されます。
		//$data = $dao->searchList($page , $fmsort ,$fminit,$fmformat ,$year4, $matrix_cd, $forms["month"] , $aja );
		$data = $dao->searchList($page , 0 ,$fminit,5,$year4, NULL, NULL , $aja );
		
		//表示ために整形開始
		$plane_sum   = 0 ;
		$now_sum     = 0 ;
		$last_sum   = 0 ;
		
		$plane_sum_qua   = 0 ;
		$now_sum_qua     = 0 ;
		$last_sum_qua   = 0 ;
		
		foreach ($data as $k => $v ) {
			$dispweek = '第'.$v["week_cd"].'週';
			if ( $v["[week_seq"] ) {
				$dispweek = $dispweek.$v["[week_seq"]."回";
			}
			$dispweek = $dispweek."(". Class_PWD::int8_to_mmdd($v["flier_date"]) .")";
			$data[$k]["week"]        = $dispweek;
			
			$plane_sum = $plane_sum +  $v["plane_cost"] + $v["plane_dm"];
			$now_sum   = $now_sum   +  $v["now_cost"]   + $v["nowe_dm"];
			$last_sum  = $last_sum  +  $v["last_cost"]  + $v["last_dm"];
			
			$data[$k]["plane_cost"]  = $plane_sum;
			$data[$k]["now_cost"]    = $now_sum;
			$data[$k]["last_cost"]   = $last_sum;
			
			$plane_sum_qua  = $plane_sum_qua  +  $v["plane_qua"];
			$now_sum_qua    = $now_sum_qua    +  $v["now_qua"];
			$last_sum_qua   = $last_sum_qua   +  $v["last_qua"];
			
			$data[$k]["plane_qua"] = $plane_sum_qua;
			$data[$k]["now_qua"]   = $now_sum_qua;
			$data[$k]["last_qua"]  = $last_sum_qua;
			
			$data[$k]["plane_li"]    = $plane_sum_qua - $plane_sum ;
			$data[$k]["now_li"]      = $now_sum_qua   - $now_sum  ;
			$data[$k]["last_li"]     = $last_sum_qua  - $last_sum ;
			
			Class_PWD::set_rate_inc_by_nm($data, $k, "cost");
			Class_PWD::set_rate_inc_by_nm($data, $k, "qua");
			Class_PWD::set_rate_inc_by_nm($data, $k, "li");
			//$data[$k] = $v;
		}
		$k = $k + 1;
		$data[$k]["week"] = "total";
		$data[$k]["plane_cost"]  = $plane_sum;
		$data[$k]["now_cost"]    = $now_sum;
		$data[$k]["last_cost"]   = $last_sum;
		
		$data[$k]["plane_qua"] = $plane_sum_qua;
		$data[$k]["now_qua"]   = $now_sum_qua;
		$data[$k]["last_qua"]  = $last_sum_qua;
		
		$data[$k]["plane_li"]    = $plane_sum_qua - $plane_sum ;
		$data[$k]["now_li"]      = $now_sum_qua   - $now_sum  ;
		$data[$k]["last_li"]     = $last_sum_qua  - $last_sum ;
		
		Class_PWD::set_rate_inc_by_nm($data, $k, "cost");
		Class_PWD::set_rate_inc_by_nm($data, $k, "qua");
		Class_PWD::set_rate_inc_by_nm($data, $k, "li");
		
	}


//表示ために整形完了
if (empty($forms["year4"])) {
	//$forms["year4"] = date('Y');
	$forms["year4"] = date('Y');
}

$smarty->assign('year4',  Class_PWD::get_years());
$week= Class_M_OTHER::get_week_array( $forms["year4"], PLEASE_SELECT_ARR, TRUE );
$smarty->assign('week', $week );
//test
if (empty($forms["week_cd"])) {
	$forms["week_cd"] = "";
}

//週名を設定する
foreach ($week as $k => $v) {

	if ($k == $martrix_cd){
		$smarty->assign('weekname', $v );
		break;
	}
}

//ゾーンとエリア名称を設定する
$aoki_area_zones = array();
$aoki_area_zone=array_reverse($aoki_area_zone,true);
foreach ($aoki_area_zone as $k => $v) {
	$aoki_area_zones[$k] = $v;
}

$smarty->assign('sorts', $aoki_area_zones );

//出力形式

/*
$outputlayoutstmp = array();
foreach ($outputlayout as $k => $v) {
	$outputlayoutstmp[$k] = $v;
}
$forms["outputlayout"]='0';
$smarty->assign('outputlayouts', $outputlayoutstmp );
*/

if( isset($zoneuser) )    $smarty->assign('zoneuser', $zoneuser);
if( isset($areauser) )    $smarty->assign('areauser', $areauser);

if( isset($forms) ) $smarty->assign('forms', $forms);
if( isset($data) )  $smarty->assign('data', $data);
//if( isset($disp) )  $smarty->assign('disp', $disp);
if( isset($page) )  $smarty->assign('page', $page);

$smarty->assign('menu', $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );
$smarty->assign('pageing', $smarty->fetch('page.html') );
$smarty->display('mcost_list_sum_index.html');
?>